package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Security;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @RequestMapping("/getUsername")
    public Result getUsername(){

        User user = null;
        try {
            user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (user!=null){
                String username = user.getUsername();
                String password = user.getPassword();
                return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.GET_USERNAME_FAIL);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user ){
        try {
            userService.add(user);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage( @RequestBody QueryPageBean queryPageBean){
       return userService.pageQuery(queryPageBean);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
           List<Integer> roleIds=userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }
    @RequestMapping("/findAllRole")
    public Result findAllRole(){
        try {
           List<Role> roles= userService.findAllRole();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(Integer id,@RequestBody Integer[]roleIds){
        try {
            userService.edit(id,roleIds);
            return  new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
    }
}
